import { Databus } from "../core/Databus";
import { UserInfo } from "../platform/UserInfo";

const databus = Databus.Instance();
export class GameDataAdaptor {
  private static instance: GameDataAdaptor = null as unknown as GameDataAdaptor;
  
  public static Instance() {
    if (GameDataAdaptor.instance) {
      return GameDataAdaptor.instance;
    }
    GameDataAdaptor.instance = new GameDataAdaptor();
    return GameDataAdaptor.instance;
  }
  constructor() { }

  // 更新本地用户金币 和 后台服务器金币数量
  public updateUserGoldCoin(coin: number) {
    new UserInfo().updateUserGoldCoin(databus.selfInfo.userId, databus.selfInfo.goldCoin, coin);
    databus.selfInfo.addGoldCoin(coin);
  }

  public updateUserGameNum(diffNum: number) {
    const winNum = databus.selfInfo.winNum;
    const loseNum = databus.selfInfo.loseNum;
    const tieNum = databus.selfInfo.tieNum;
    new UserInfo().updateUserGameNum(databus.selfInfo.userId, diffNum,
      winNum, loseNum, tieNum,
    );

    databus.selfInfo.addGameNum(diffNum);
  }

  public updateUserPlayLevel(diffLevel: number) {
    new UserInfo().updateUserPlayLevel(databus.selfInfo.userId, databus.selfInfo.playLevel.playLevel, diffLevel);
    databus.selfInfo.playLevel.addLevel(diffLevel);
  }
}

